import {
	API_URL
} from '@/env.js'
import {
	router,
	RouterMount
} from "@/common/router.js"
const imageList = [];
const baseImageList = [];
export default {
	// 选择图片
	/**
	 * count        最多可以选择的图片张数
	 * sizeType     original 原图，compressed 压缩图
	 * sourceType   album 从相册选图，camera 使用相机
	 */
	//选择图片
	chooseImage(count = 1) {
		return new Promise((resolve, reject) => {
			uni.chooseImage({
				count: count,
				sizeType: ['original', 'compressed'],
				sourceType: ['album'],
				success: res => {
					resolve(res.tempFilePaths);
				}
			})
		})
	},
	// 支付宝转码
	alypayBase64(imagePath) {
		let that = this;
		return new Promise((resolve, reject) => {
			my.getImageInfo({
				src: imagePath,
				success: async (res) => {
					console.log(res, 'sucess')
					this.imgWidth = res.width / 2.5;
					this.imgHeight = res.height / 2.5;
					let canvas = my.createCanvasContext("canvas");
					canvas.drawImage(imagePath, 0, 0, this.imgWidth, this
						.imgHeight); // 1. 绘制图片至canvas
					console.log(canvas, 'canvas');
					// 绘制完成后执行回调
					canvas.draw(false, async () => {
						console.log('canvas.draw');
						let base64 = await canvas.toDataURL({
							width: this.imgWidth,
							height: this.imgHeight,
							quality: 1,
						});
						this.base64 = base64;
						// console.log(base64,'base64');
						base64 = base64.replace('data:image/png;base64,', '');
						console.log(base64, 'base64');
						resolve(base64);
					});
				},
			});
		})
	},
	// 支付宝转码
	alypayBases64(imagePath) {
		let that = this;
		let baselist = [];
		let base64 = '';
		console.log(imagePath, 'imagePath')
		return new Promise((resolve, reject) => {
			imagePath.map((item, index) => {
				uni.getImageInfo({
					src: item,
					success: async (res) => {
						console.log(res, 'sucess')
						this.imgWidth = res.width / 2.5;
						this.imgHeight = res.height / 2.5;
						let canvas = uni.createCanvasContext("canvas");
						canvas.drawImage(item, 0, 0, this.imgWidth, this
							.imgHeight); // 1. 绘制图片至canvas
						console.log(canvas, 'canvas');
						// 绘制完成后执行回调
						canvas.draw(false, async () => {
							console.log('canvas.draw');
							base64 = await canvas.toDataURL({
								width: this.imgWidth,
								height: this.imgHeight,
								quality: 1,
							});
							this.base64 = base64;
							base64 = base64.replace('data:image/png;base64,',
								'');
							baselist.push(base64)
						});
					},
				});
				console.log(base64, 'base64-2')
				console.log(baselist, 'baselist');
				resolve(baselist);
			})
		})
	},
	// 上传图片
	uploadImages(api, url) {
		let config_url = API_URL;
		console.log(config_url + api, 'url');
		console.log(url, 'upload');
		uni.showLoading({
			title: '上传中'
		});
		return new Promise((resolve, reject) => {
			/**
			 *  url 服务器地址
			 *  fileType 支付宝小程序必填
			 *  filePath 要上传文件的路径
			 *  name 文件对应的key值，在服务端通过这个key值可以获取到二进制内容
			 *  success 回调函数
			 */
			uni.uploadFile({
				url: config_url + api,
				// #ifdef MP-ALIPAY 
				fileType: 'image', // 支付宝小程序必填
				// #endif
				filePath: url,
				name: 'file', // 根据后台进行修改
				success: res => {
					if (res.status === 200) {
						uni.hideLoading();
						uni.showToast({
							title: '上传成功',
							icon: 'none'
						});
						resolve(res.data);
					} else {
						uni.hideLoading();
						uni.showModal({
							title: '上传失败',
						});
					}
				},
				fail: e => {
					console.log(e, 'e');
				}
			})
		})
	},
	// 图片地址转成base64
	getFileSystemManager(url) {
		return new Promise((resolve, reject) => {
			uni.getFileSystemManager().readFile({
				filePath: url,
				encoding: "base64",
				success: res => {
					resolve(res)
				},
				fail: e => {
					reject(e)
				}
			})
		})
	},
	// 打电话
	callphone(phoneNumber = '') {
		let num = phoneNumber.toString();
		uni.makePhoneCall({
			phoneNumber: num,
			fail(err) {
				console.log('makePhoneCall有问题', err);
			}
		})
	},
	/**
	 * 弱提示框 - 支付宝小程序与微信小程序存在兼容性问题.详情看注解
	 * title 标题
	 * icon  图标  success loading  none
	 * duration 延迟 0为不关闭，毫秒数 
	 */
	toast(title, icon = "none", duration = 1500) {
		// #ifdef MP-WEIXIN
		uni.showToast({
			title: title,
			icon: icon,
			duration: duration
		})
		// #endif
		/**
		 * type 不支持loading  支持success fail none （exception 类型必须传文字)
		 */
		// #ifdef MP-ALIPAY
		my.showToast({
			content: title,
			type: icon,
			duration: duration
		})
		// #endif
	},
	// 复制至剪切板
	setClipboard(data) {
		uni.setClipboardData({
			data: data,
			success: function() {
				this.toast('已复制至剪切板')
			}
		})
	},


}
